package dryven.unittest.validation;

import java.util.Date;

import dryven.i18n.Translator;
import dryven.model.di.DependencyService;
import dryven.model.validation.ValidationException;
import dryven.model.validation.ValidationFieldDescription;
import dryven.model.validation.ValidatorService;
import dryven.model.validation.ValidatorSession;
import dryven.model.validation.di.DependencyInjectingValidatorService;
import dryven.model.validation.validators.DateValidator;
import dryven.model.validation.validators.DefaultValidator;
import dryven.unittest.mocks.MockDependencyService;
import dryven.unittest.validation.mockup.MockupValidationFormValueCollection;
import dryven.unittest.validation.mockup.ValidatorFieldDescriptionMockup;
import dryven.unittest.validation.mockup.ValidatorFormMockup;
import dryven.unittest.validation.mockup.ValidatorReporterMockup;
import junit.framework.TestCase;

public class ValidatorTestCase extends TestCase {
	public void testDate() {
		DateValidator validator = new DateValidator();
		ValidatorFieldDescriptionMockup desc = new ValidatorFieldDescriptionMockup(true,10,"birthday",Date.class);
		ValidatorFormMockup form = new ValidatorFormMockup();
		form.addValue("birthday", "31/03/1983");
		ValidatorReporterMockup reporter = new ValidatorReporterMockup();
		
		validator.validate(desc, form, reporter);
		assertTrue(reporter.hasValue());
		assertFalse(reporter.hasError());
		assertTrue(reporter.getConvertedValue().equals(new Date(1983, 3, 31)));
		
		form = new ValidatorFormMockup();
		form.addValue("birthday", "1/2/3/2010");
		reporter = new ValidatorReporterMockup();
		
		validator.validate(desc, form, reporter);
		assertFalse(reporter.hasValue());
		assertTrue(reporter.hasError());
		
		form = new ValidatorFormMockup();
		form.addValue("birthday_day", "31");
		form.addValue("birthday_month", "03");
		form.addValue("birthday_year", "1983");
		reporter = new ValidatorReporterMockup();
		
		validator.validate(desc, form, reporter);
		assertTrue(reporter.hasValue());
		assertFalse(reporter.hasError());
		assertTrue(reporter.getConvertedValue().equals(new Date(1983, 3, 31)));
		
		form = new ValidatorFormMockup();
		form.addValue("birthday_day", "a");
		form.addValue("birthday_month", "b");
		form.addValue("birthday_year", "cccc");
		reporter = new ValidatorReporterMockup();
		
		validator.validate(desc, form, reporter);
		assertFalse(reporter.hasValue());
		assertTrue(reporter.hasError());
		
		desc = new ValidatorFieldDescriptionMockup(false,10,"birthday",Date.class);
	}
	
	public void testValidator() throws ValidationException {
		DependencyService diService = new MockDependencyService();
		ValidatorService service = new DependencyInjectingValidatorService(diService, new Translator() {
			@Override
			public String translate(String string, Object... params) {
				return "no translation available";
			}
		});
		
		MockupValidationFormValueCollection form = new MockupValidationFormValueCollection();
		form.put("amount", "35");
		
		ValidationFieldDescription fd = new ValidationFieldDescription() {
			@Override
			public boolean isRequired() {return false;}
			@Override
			public Class<?> getValueType() {return Integer.TYPE;}
			@Override
			public String getName() {return "amount";}
			@Override
			public int getMaxLength() {return 8;}
		};
		ValidatorSession session = service.newSession(null);
		Object value = session.validate(fd, form, DefaultValidator.class );
		assertTrue((Integer)value==35);
		
		form.put("amount", "ab");
		boolean exceptionCaught = false;
		try {
			value = session.validate(fd, form, DefaultValidator.class );
		} catch(ValidationException e) {
			exceptionCaught = true;
		}
		assertTrue(exceptionCaught);
		assertTrue(session.hasErrors());
		int errormsgcount = 0;
		for (String msg : session.getErrorMessages().getMessageNames()) {
			++errormsgcount;
		}
		assertTrue(errormsgcount==1);
	}
}